PostGIS
Back to homepage

Spatial SQL for PostgreSQL

PostGIS is the open-source spatial extension for PostgreSQL. Store, index, query, and analyze geometry, geography, and raster data with SQL.

PostGIS 3.7.0 release

Find an address

Tweed Courthouse stands behind City Hall at 52 Chambers St. It houses New York City’s Department of Education.
SQL
begin;
set local search_path to tiger, public;

-- Address → coordinates
select ST_AsText(geomout, 6) as point
from tiger.geocode(
  '52 Chambers St, New York, NY 10007', 1
);

-- Coordinates → address
select tiger.pprint_addy(addy[1]) as address
from tiger.reverse_geocode(
  ST_Point(-74.005542, 40.713402, 4326)
);
commit;

Tiger Geocoder requires New York TIGER data. The reverse lookup uses the city’s address point.

Find the stops within 500 m

11 stop records within 500 m. The buffer covers about 0.78 km².
SQL
-- Index the stop geometries.
create index if not exists nyc_subway_stations_geom_idx
on nyc_subway_stations using gist (geom);

-- Use the same CRS as the stops.
with address(point) as (select
  ST_Transform(ST_Point(-74.005542, 40.713402, 4326), 26918)
)
-- Filter by distance in meters.
select name, ST_Distance(geom, point) as meters
from nyc_subway_stations, address
where ST_DWithin(geom, point, 500)
order by meters, name;

The GiST index can speed up the search. Try it in the workshop.

Group stops by neighborhood

4 neighborhoods meet the circle. Stops match the color of their neighborhood. Chinatown has no stops within the circle.
SQL
with address(point) as (values (
  ST_Point(583997.30, 4507418.26, 26918)
))
-- Keep neighborhoods with zero stops.
select n.name, count(s.geom) as stops
from nyc_neighborhoods n cross join address
left join nyc_subway_stations s
  on ST_Intersects(s.geom, n.geom)
  and ST_DWithin(s.geom, point, 500)
where ST_DWithin(n.geom, point, 500)
group by n.name
order by stops desc, n.name;

Map subway coverage

Merge the areas around the selected stops to see which parts of the search area are close to the subway. Distances are measured in a straight line.
SQL
with address(point) as (values (
  ST_Point(583997.30, 4507418.26, 26918)
)), coverage as (
  -- Buffer all nearby stops as one multipoint.
  select ST_Buffer(point, 500, 64) as zone,
         ST_Buffer(ST_Collect(geom), 150) as reach
  from nyc_subway_stations, address
  where ST_DWithin(geom, point, 500)
  group by point
)
-- Split the search area.
select ST_Intersection(zone, reach) as covered,
       ST_Difference(zone, reach) as gaps
from coverage;

Add elevation to the map

ST_SetZ gives the address point a Z coordinate using a USGS 3DEP raster.
SQL
with address(point) as (values (
  ST_Point(583997.30, 4507418.26, 26918)
)), elevation as (
  -- Copy the elevation pixel to the point's Z.
  select ST_SetZ(rast, point) as point_z
  from nyc_elevation, address
  where ST_Intersects(rast, point)
    and ST_Value(rast, point) is not null
)
select round(ST_Z(point_z)::numeric, 1) as meters,
       point_z
from elevation;

Load the 30 m GeoTIFF as nyc_elevation. The profile samples Chambers St from west to east.

Publish the stops

Publish the nearby stops that fall inside one tile. The inset shows their position in tile coordinates.
SQL
-- Get tile bounds in EPSG:3857.
with tile as (
  select ST_TileEnvelope(16, 19295, 24640) as bounds
), stops as (
  -- Project and clip the nearby stops to the tile.
  select name, ST_AsMVTGeom(
    ST_Transform(geom, 3857), bounds, buffer => 0
  ) as geom
  from nyc_subway_stations, tile
  where ST_DWithin(geom,
    ST_Point(583997.30, 4507418.26, 26918), 500)
)
select ST_AsMVT(stops, 'stops') from stops;